Completed
Push — master ( 60f354...0d0dec )
by hung
11s
created

input.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 1
1
const fs = require('fs')
2
const { join } = require('path')
3
const { Readable } = require('stream')
4
5
module.exports = (input, callback) => {
6
  if (typeof input === 'string') {
7
    // Input as file
8
    const path = join(__dirname, '../', input)
9
    if (!fs.existsSync(path)) {
10
      console.error(`Cannot read file ${path}`)
11
      return
12
    }
13
    const html = fs.readFileSync(path, 'utf8')
14
    callback(html)
15
  } else if (input instanceof Readable) {
16
    // Input as stream
17
    input.on('data', chunk => {
18
      callback(chunk.toString())
19
    })
20
    input.on('error', err => {
21
      console.error(err)
22
    })
23
  } else {
24
    // Unknown input
25
    console.error('Invalid Input')
26
  }
27
}
28